Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | export const dynamic = "force-dynamic"; /** * Dev Milestone Detail API * GET /api/dev/milestones/[id] - Get a single milestone with stats * PATCH /api/dev/milestones/[id] - Update a milestone * DELETE /api/dev/milestones/[id] - Delete a milestone */ import { NextRequest, NextResponse } from 'next/server'; import { Session } from "next-auth"; import { withAdmin, withErrorHandling, successResponse, ApiError, ApiSuccessResponse, ApiErrorResponse } from "@/lib/api"; import { AuthenticatedUser } from "@/lib/api/middleware"; import { prisma } from '@/lib/prisma'; import { UpdateDevMilestoneSchema } from '@/lib/validation/dev-ticket-schemas'; import { getMilestoneProgress } from '@/lib/dev-ticket'; import { logger } from '@/lib/logging'; interface RouteParams { params: Promise<{ id: string }>; } async function handleGet( request: NextRequest, context: unknown ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const milestone = await prisma.devMilestone.findUnique({ where: { id }, include: { project: { select: { id: true, name: true, key: true, color: true } }, tickets: { include: { assignee: { select: { id: true, name: true, email: true, image: true } }, labels: true }, orderBy: [{ priority: 'desc' }, { createdAt: 'asc' }] }, _count: { select: { tickets: true } } } }); if (!milestone) { throw ApiError.notFound('Milestone not found'); } // Get progress stats const progress = await getMilestoneProgress(id); // Get ticket breakdown by status const ticketsByStatus = await prisma.devTicket.groupBy({ by: ['status'], where: { milestoneId: id }, _count: true }); return successResponse({ ...milestone, stats: { ...progress, byStatus: Object.fromEntries(ticketsByStatus.map((s) => [s.status, s._count])) } }); } async function handlePatch( request: NextRequest, context: unknown, session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const body = await request.json(); const validationResult = UpdateDevMilestoneSchema.safeParse(body); if (!validationResult.success) { throw ApiError.validation( 'Validation failed', validationResult.error.flatten().fieldErrors ); } // Check if milestone exists const existingMilestone = await prisma.devMilestone.findUnique({ where: { id } }); if (!existingMilestone) { throw ApiError.notFound('Milestone not found'); } const data = validationResult.data; // Build update data const updateData: Record<string, unknown> = { ...data }; // If completing milestone, set completedAt if (data.status === 'COMPLETED' && existingMilestone.status !== 'COMPLETED') { updateData.completedAt = new Date(); } // If reopening, clear completedAt if (data.status && data.status !== 'COMPLETED' && existingMilestone.completedAt) { updateData.completedAt = null; } // Update milestone const milestone = await prisma.devMilestone.update({ where: { id }, data: updateData, include: { project: { select: { id: true, name: true, key: true, color: true } }, _count: { select: { tickets: true } } } }); logger.info(`Updated milestone "${milestone.name}"`, { category: 'DEV_MILESTONES', milestoneId: id, userId: user.id, changes: Object.keys(data) }); return successResponse(milestone); } async function handleDelete( request: NextRequest, context: unknown, session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; // Check if milestone exists const milestone = await prisma.devMilestone.findUnique({ where: { id }, include: { _count: { select: { tickets: true } } } }); if (!milestone) { throw ApiError.notFound('Milestone not found'); } // Remove milestone reference from tickets (don't delete tickets) if (milestone._count.tickets > 0) { await prisma.devTicket.updateMany({ where: { milestoneId: id }, data: { milestoneId: null } }); } // Delete milestone await prisma.devMilestone.delete({ where: { id } }); logger.info(`Deleted milestone "${milestone.name}"`, { category: 'DEV_MILESTONES', milestoneId: id, userId: user.id, ticketsUnlinked: milestone._count.tickets }); return successResponse({ message: 'Milestone deleted successfully', ticketsUnlinked: milestone._count.tickets }); } export const GET = withErrorHandling(withAdmin(handleGet)); export const PATCH = withErrorHandling(withAdmin(handlePatch)); export const DELETE = withErrorHandling(withAdmin(handleDelete)); |